package edu.study.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.AuthApi;
import edu.study.user.api.dto.AuthAndSubDto;
import edu.study.user.api.dto.AuthDto;
import edu.study.user.api.req.auth.AddAuthReq;
import edu.study.user.api.req.auth.DeleteAuthReq;
import edu.study.user.api.req.auth.ModifyAuthReq;
import edu.study.user.api.req.auth.ModifyAuthStatusReq;
import edu.study.user.api.req.auth.QueryAuthPageListReq;
import edu.study.user.api.req.auth.QueryAuthReq;
import edu.study.user.service.AuthService;

@RestController
public class AuthApiImpl implements AuthApi {

	@Autowired
    private Lock lock;

    @Autowired
    private AuthService authService;

	@Override
	public BaseRespDto addAuth(@RequestBody AddAuthReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getAuthName(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = authService.addAuth(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto deleteAuth(@RequestBody DeleteAuthReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getAuthNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = authService.deleteAuth(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyAuth(@RequestBody ModifyAuthReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getAuthNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = authService.modifyAuth(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public BaseRespDto modifyAuthStatus(@RequestBody ModifyAuthStatusReq req) {
		BaseRespDto resp = lock.lockAndExecute(req.getAuthNo(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = authService.modifyAuthStatus(req);
                return resp1;
            }
        });
		return resp;
	}

	@Override
	public RespDto<AuthDto> queryAuth(@RequestBody QueryAuthReq req) {
		RespDto<AuthDto> resp = authService.queryAuth(req);
        return resp;
	}

	@Override
	public PageRespDto<AuthAndSubDto> queryAuthPageList(@RequestBody QueryAuthPageListReq req) {
		PageRespDto<AuthAndSubDto> resp = authService.queryAuthPageList(req);
        return resp;
	}
	
	@Override
	public RespDto<List<AuthDto>> queryAuthAndSubList(@RequestBody QueryAuthReq req) {
		RespDto<List<AuthDto>> resp = authService.queryAuthAndSubList(req);
        return resp;
	}
	
	@Override
	public RespDto<AuthAndSubDto> queryAuthAndSub(@RequestBody QueryAuthReq req) {
		RespDto<AuthAndSubDto> resp = authService.queryAuthAndSub(req);
        return resp;
	}
}
